<?php

namespace App\Repositories\Admin;

use App\Models\Admin\CompanyModel;
use App\Models\Admin\StaffModel;
use App\Models\Admin\UserModel;
use App\Models\Main\UserNoticeModel;
use App\Services\Jiguang\JMessageApi;
use Hash;
use App\Repositories\BaseRepository;

class UserRepository extends BaseRepository {

    protected $repository;

    public function __construct() {
        $this->repository = new UserModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList() {
        $models = $this->repository->orderBy('id', 'desc')
                ->paginate(10);
        $models->limit = 10;
        return $models;
    }

    public function setAdd($data) {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data, $id) {
        $data['updated_at'] = time();
        return $this->repository->where('id', $id)
                        ->update($data);
    }

    public function setPwdByUid($id, $pwd) {
        \App\Services\Jiguang\JMessageApi::updatePassword(JMessageApi::idToJiguang($id),$md5=md5($pwd.time()));
        return $this->repository->where('id', $id)
                        ->update(array(
                            'password' => $pwd,
                            'jiguang_pass'=>$md5
                        ));
    }

    /**
     * 返回用户类型名称
     */
    public function getTypeName(  &$model) {
        return '弃用';
        $type = $model->type;
        //type：1个人用户；2仅公司；3仅工厂；4公司含工厂
        if ($type == 1) {
            //cate：1工厂，2公司
            $staff = StaffModel::where('uid', $model->id)->first();
            $model->typeName = ($staff && $staff->cate == 1) ? '工厂员工' : '公司员工';
        } else {
            //cate：1仅工厂，2仅公司，3公司含工厂
            $com = CompanyModel::where('uid', $model->id)->first();
            $facType = $com ? $com->facType : 0;
            $cateModel = $this->getCateNameById($facType);
            if ($type == 2) {
                $model->typeName = $cateModel ? $cateModel->name : '仅公司';
            } else if ($type == 3) {
                $model->typeName = $cateModel ? $cateModel->name : '仅工厂';
            } else if ($type == 4) {
                $model->typeName = $cateModel ? $cateModel->name : '公司含工厂';
            } else {
                $model->typeName = '未知企业';
            }
        }
        return $model->typeName;
    }

    public function getShowOne($id) {
        $model = $this->repository
                ->where('id', $id)
                ->first();
        if (!$model) {
            return '';
        }
        $this->getTypeName($model);
        return $model;
    }

    public function getShowOneByMobile($mobile) {
        $model = $this->repository->where('phone', $mobile)->first();
        if (!$model) {
            return '';
        }
        /*
        //判断主体类型，以及资料完善情况
        if ($model->type == 1) {
            $staffRepository = new StaffRepository();
            $staffModel = $staffRepository->getShowOneByUid($model->id);
            $model->cate = $staffModel ? $staffModel->cate : 0;
        } else if (in_array($model->type, [2, 3, 4])) {
            $comRepository = new CompanyRepository();
            $comModel = $comRepository->getShowOneByUid($model->id);
            $model->cate = $comModel ? $comModel->cate : 0;
        } else {
            $model->cate = 0;
        }*/
        return $model;
    }

    /**
     * 通过用户名，获取用户
     */
    public function getOneByName($name) {
        return $this->repository->where('name', $name)->first();
    }

    /**
     * 通过UID，获取个人/企业
     */
    public function getInfoByUid($uid, $type) {
        if ($type == 1) {
            $model = StaffModel::where('uid', $uid)->first();
        } else {
            $model = CompanyModel::where('uid', $uid)->first();
        }
        return $model ? $model->id : 0;
    }

    /**
     * 密码重置
     */
    public function setPwd($mobile, $pwdHash) {
        return $this->repository->where('phone', $mobile)
                        ->update(array(
                            'password' => $pwdHash,
                            'updated_at' => time(),
        ));
    }

    /**
     * 密码重置
     */
    public function setTypeByUid($uid, $data) {
        return $this->repository->where('id', $uid)
                        ->update($data);
    }

    /**
     * 设置坐标
     */
    public function setPointByUid($uid, $point) {
        return $this->repository->where('id', $uid)
                        ->update(array('point' => $point));
    }

    /**
     * 设置简历是否被搜索
     */
    public function setResume($uid, $resume) {
        if (!in_array($resume, [0, 1])) {
            return '';
        }
        $model = $this->repository->where('id', $uid)->first();
        if (!$model || $model->is_resume == $resume) {
            return '';
        }
        return $this->repository->where('id', $uid)
                        ->update(['is_resume' => $resume]);
    }

    /**
     * 用户类型
     */
    public function getTypeList() {
        return $this->repository->getTypes();
    }

    /**
     * 用户类型名称
     */
    public function getTypeNameByType($type) {
        $types = $this->repository->getTypes();
        return array_key_exists($type, $types) ? $types[$type] : '';
    }

    /**
     * 设置用户删除
     */
    public function setDel($id) {
        return $this->repository
                        ->where('id', $id)
                        ->where('is_delete', 0)
                        ->update(['is_delete' => 1]);
    }

    /**
     * 账户余额获取
     */
    public function getUserFinance($uid) {
        $model = $this->repository->where('id', $uid)->first();
        return array(
            'finance' => $model ? $model->finance : 0,
            'income' => $model ? $model->income : 0,
        );
    }

    /**
     * 账户余额充值，设置
     */
    public function setFinanceIncome($uid, $money) {
        $model = $this->repository->where('id', $uid)->first();
        if (!$model) {
            return false;
        }
        $financeNew = ($model->finance * 100 + $money * 100) / 100;
        $this->repository->where('id', $uid)
                ->update(array(
                    'finance' => $financeNew,
        ));
        return true;
    }

    /**
     * 账户余额提现，设置
     */
    public function setFinanceOut($uid, $money) {
        $model = $this->repository->where('id', $uid)->first();
        if (!$model) {
            return false;
        }
        $finance = $model->finance * 100;
        $money2 = $money * 100;
        if ($finance < $money2) {
            return false;
        }
        $fiannceNew = ($model->finance * 100 - $money * 100) / 100;
        $this->repository->where('id', $uid)
                ->update(array(
                    'finance' => $fiannceNew,
        ));
        return true;
    }

    /**
     * 账户交易，减少付款人余额
     */
    public function setPayerFinance($uid, $money) {
        $model = $this->repository->where('id', $uid)->first();
        if (!$model) {
            return false;
        }
        $finance = $model->finance * 100;
        $money2 = $money * 100;
        if ($finance < $money2) {
            return false;
        }
        $fiannceNew = ($model->finance * 100 - $money * 100) / 100;
        $this->repository->where('id', $uid)
                ->update(array(
                    'finance' => $fiannceNew,
        ));
        return true;
    }

    /**
     * 账户交易，增加收款人余额、总收益
     */
    public function setReceiver($uid, $money) {
        $model = $this->repository->where('id', $uid)->first();
        if (!$model) {
            return false;
        }
        $fiannceNew = ($model->finance * 100 + $money * 100) / 100;
        $incomeNew = ($model->income * 100 + $money * 100) / 100;
        $this->repository->where('id', $uid)
                ->update(array(
                    'finance' => $fiannceNew,
                    'income' => $incomeNew,
        ));
        return true;
    }

    /**
     * 用户须知
     */
    public function setUserNotice($uid) {
        $model = UserNoticeModel::where('uid', $uid)->first();
        if ($model) {
            return false;
        }
        $lastId = UserNoticeModel::insertGetId(array(
                    'uid' => $uid,
                    'created_at' => time(),
        ));
        return $lastId;
    }

    /**
     * 平台交易密码设置
     */
    public function setTradePwd($uid, $pwd) {
        $model = $this->repository->where('id', $uid)->first();
        if (!$model) {
            return false;
        }
        $this->repository
                ->where('id', $uid)
                ->update(array('tradePwd' => $pwd));
        return true;
    }

    /**
     * 清空用户数据表
     */
    public function setClean()
    {
        $resStaff = StaffModel::truncate();
        if (!$resStaff) { return -1; }
        $resCom = CompanyModel::truncate();
        if (!$resCom) { return -2; }
        $resUser = UserModel::truncate();
        if (!$resUser) { return -3; }
        return 0;
    }
}